package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.mapper.OrderMapper;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.health.service.OrderSettingService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.itheima.health.utils.date.DateUtils;

import java.sql.Date;
import java.util.Map;


@Service
@Transactional
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {
    //日期表
    @Autowired
    private OrderSettingService orderSettingService;
    //会员表
    @Autowired
    private MemberService memberService;

    @Override
    public Order add(Map map) {
        System.out.println("执行到了add");
        //需要判断预约日期  是否预约满  是否重复预约  添加订单  用户是否会员
        //1.判断用户选择天数是否可以预约(预约满了 或者不能预约(休息), )  查询最大预约数
        //2.判断是否重复预约 根据用户手机与订单表判断
        //3.判断用户表中是否有该用户信息  无->新会员  需要录入  有->老会员 判断是否重复预约

        //t_order  订单表

        String orderDate = (String) map.get("orderDate");  //日期
        String tId = (String) map.get("setmealId");  //套餐id
        Integer tIdI = Integer.valueOf((String) map.get("setmealId"));//套餐id
//                (Integer) map.get("setmealId");
        String orderType = (String) map.get("orderType");  //预约类型

        Order order = new Order();  //预约添加订单表需要的对象
        //订单id  表自增
        //预约日期 表单提供
        //预约方式  默认微信预约
        //到诊状态
        //套餐id
        order.setOrderDate(DateUtils.parseString2Date(orderDate, "yyyy-MM-dd"));
        order.setOrderType(orderType);
        order.setOrderStatus("未到诊");
        order.setSetmealId(tIdI);


        int count = orderSettingService.isOrderSetting(orderDate);
        System.out.println(count + "业务处理");

        if (count > 0) {
            System.out.println("日期可以预约");

            //可以预约  还要判断是否重复预约
            String telephone = (String) map.get("telephone");
            //查询的预约用户(查询会员)
            Member member = memberService.findByPhone(telephone);


            if (member == null) {
                //新会员 (添加会员表)
                //添加对象  这里完善部分数据 其他数据后期完善  姓名  性别   身份证号  电话号码  注册日期   前台表单提供
                String name = (String) map.get("name");
                String sex = (String) map.get("sex");
                String idCard = (String) map.get("idCard");
                member = new Member();
                member.setName(name);
                member.setSex(sex);
                member.setPhoneNumber(telephone);
                member.setIdCard(idCard);
                member.setRegTime(new Date(System.currentTimeMillis()));

                memberService.save(member);

                //继续预约
                order.setMemberId(member.getId());
                baseMapper.insert(order);

            } else {
                //老会员 查询订单表 判断是否重复预约(会员id查询订单表)
                //是老会员 那么使用会员id
                int i = memberService.isOrderDayByMemberIdBySetmealId(orderDate, member.getId(), Integer.parseInt(tId));  //查询订单表中是否有该用户订单,套餐 在预约天
                if (i > 0) {
                    //说明该用户已经预约过
                    throw new RuntimeException("请勿重复预约");
                } else {
                    //进行预约  更新订单表
                    order.setMemberId(member.getId());
                    baseMapper.insert(order);
                }
            }
        } else {
            //不可预约
            throw new RuntimeException("预约已满,请选择新日期");
        }

        orderSettingService.upDateReservationsByOrderFate(orderDate);
        return order;
    }


    //预约成功页面展示预约信息
    @Override
    public Map findfindOrderById(String id) {
        Map map = baseMapper.findfindOrderById(id);
        return map;
    }


}
